﻿using System;
using System.Text;

namespace dotHTML.HTML {
    /// <summary>
    /// Represents an image.
    /// </summary>
    [Serializable]
    [Standalone]
    public class Img : Element {
        /// <summary>
        /// Determines where to align the image with respect to the content.
        /// </summary>
        public enum Alignment {
            /// <summary>
            /// Aligns the image to the left of the content.
            /// </summary>
            Left,
            /// <summary>
            /// Aligns the image to the right of the content.
            /// </summary>
            Right,
            /// <summary>
            /// Aligns the image in the center of the content.
            /// </summary>
            Middle,
            /// <summary>
            /// Aligns the image at the top of the content.
            /// </summary>
            Top,
            /// <summary>
            /// Aligns the image at the bottom of the content.
            /// </summary>
            Bottom
        }

        /// <summary>
        /// Determines how cross-origin requests to fetch the image should be
        /// handled.
        /// </summary>
        public enum CrossOriginChoices {
            /// <summary>
            /// No credentials are sent in the fetch.
            /// </summary>
            Anonymous,
            /// <summary>
            /// Credentials are sent in the fetch.
            /// </summary>
            UseCredentials
        }

        private Alignment? _align;
        private string _alt;
        private int? _border;
        private CrossOriginChoices? _crossorigin;
        private string _height;
        private int? _hspace;
        private bool _ismap;
        private string _longdesc;
        private string _name;
        private string _src;
        private string _usemap;
        private int? _vspace;
        private string _width;

        /// <summary>
        /// Constructs a new Img element with the given source URL.
        /// </summary>
        /// <param name="src">The source URL of this image.</param>
        public Img(string src) {
            _src = src;
        }

        /// <summary>
        /// Determines the alignment of the image with respect to its
        /// surrounding content.
        /// </summary>
        [Obsolete("Deprecated in HTML4 - use CSS instead.")]
        public Alignment? Align {
            get { return _align; }
            set { _align = value; }
        }

        /// <summary>
        /// Determines alternate text to be displayed if the image cannot be
        /// rendered.
        /// </summary>
        public string Alt {
            get { return _alt; }
            set { _alt = value; }
        }

        /// <summary>
        /// Determines the width of the border around the image, in pixels.
        /// </summary>
        [Obsolete("Deprecated in HTML4 - use CSS instead.")]
        public int? Border {
            get { return _border; }
            set { _border = value; }
        }

        /// <summary>
        /// Determines how cross-origin requests for this image are handled
        /// (whether credentials are passed or not).
        /// </summary>
        public CrossOriginChoices? CrossOrigin {
            get { return _crossorigin; }
            set { _crossorigin = value; }
        }

        /// <summary>
        /// Determines the height of the image.
        /// </summary>
        /// <remarks>Implemented as a string so that percentages can be
        /// specified (HTML4-only).</remarks>
        public string Height {
            get { return _height; }
            set { _height = value; }
        }

        /// <summary>
        /// Determines the amount of horizontal whitespace to add to the image.
        /// </summary>
        [Obsolete("Deprecated in HTML4 - use CSS instead.")]
        public int? HSpace {
            get { return _hspace; }
            set { _hspace = value; }
        }

        /// <summary>
        /// If set, indicates that this image is part of a server-side map.
        /// </summary>
        public bool IsMap {
            get { return _ismap; }
            set { _ismap = value; }
        }

        /// <summary>
        /// Determines the URL of a long description of this image.
        /// </summary>
        public string LongDesc {
            get { return _longdesc; }
            set { _longdesc = value; }
        }

        /// <summary>
        /// Determines the name of this image.
        /// </summary>
        [Obsolete("Deprecated in HTML4 - use \"id\" instead.")]
        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// Determines the source URL of this image.
        /// </summary>
        public string Src {
            get { return _src; }
            set { _src = value; }
        }

        /// <summary>
        /// The partial URL (starting with '#') of an image map associated 
        /// with the element. 
        /// </summary>
        public string UseMap {
            get { return _usemap; }
            set { _usemap = value; }
        }

        /// <summary>
        /// Determines the amount of vertical whitespace to add to the image.
        /// </summary>
        [Obsolete("Deprecated in HTML4 - use CSS instead.")]
        public int? VSpace {
            get { return _vspace; }
            set { _vspace = value; }
        }

        /// <summary>
        /// Determimes the width of the image.
        /// </summary>
        /// <remarks>Implemented as a string so that percentages can be
        /// specified (HTML4 only).</remarks>
        public string Width {
            get { return _width; }
            set { _width = value; }
        }

        /// <summary>
        /// Gets a formatted string of all local attributes of this element
        /// (not including the base attributes).
        /// </summary>
        /// <param name="sb">The StringBuilder to write this element's
        /// attribute data to.</param>
        protected override void WriteLocalAttributes(StringBuilder sb) {
            AttributeWriter.Write(sb, _align, "align", true);
            AttributeWriter.Write(sb, _alt, "alt");
            AttributeWriter.Write(sb, _border, "border");
            if (_crossorigin != null) {
                if (_crossorigin.Value == CrossOriginChoices.Anonymous) {
                    AttributeWriter.Write(sb, "anonymous", "crossorigin");
                } else {
                    AttributeWriter.Write(sb, "use-credentials", "crossorigin");
                }
            }
            AttributeWriter.Write(sb, _height, "height");
            AttributeWriter.Write(sb, _hspace, "hspace");
            AttributeWriter.Write(sb, _ismap, "ismap");
            AttributeWriter.Write(sb, _longdesc, "longdesc");
            AttributeWriter.Write(sb, _name, "name");
            AttributeWriter.Write(sb, _src, "src");
            AttributeWriter.Write(sb, _usemap, "usemap");
            AttributeWriter.Write(sb, _vspace, "vspace");
            AttributeWriter.Write(sb, _width, "width");
        }
    }
}
